#ifndef _TIFFMERGER_H_

#define _TIFFMERGER_H_

//#ifdef WIN32
//#define snprintf _snprintf
//#endif

#include "stdafx.h"
#include <tiffio.h>
#include <jpeglib.h>
#include <libexif/exif-data.h>
#include <string>

using std::string;

void MergeTiff(const CString& parentPath);

void DoMerge(const CString& szPath);

extern DWORD osDWMajorVersion;


// If it's the directory, instead of its subdirectories,
// that contains tiff files, then return true, or false
bool containTiffOrJpegDirect(const CString& szPath);

// If the file has a suffix .tiff or tif, return true, or false
bool IsTiffFile(const CString& fileName);

// If the file has a suffix ..jpg, .jpeg, .jpe .jif, .jfif, .jfi, return true, or false
bool IsJpegFile(const CString& fileName);

// Error handler of libtiff
void TIFFError(const char* /*module*/, const char* fmt, va_list argptr);

// Error handler of libjpeg
void JPEGError(j_common_ptr cinfo);

// Merge non-color image
void MergeNonColorImage(TIFF* input, TIFF* output, uint32& directoryNumber, uint32 directoryCount);

// Merge color image
void MergeColorImage(TIFF* input, TIFF* output, uint32& directoryNumber, uint32 directoryCount);

// Delete the directory's content and itself
void RecursiveRemoveDirectory(const CString& szPath);

// Merge a jpeg picture using libjpeg at www.ijg.org
void mergeJpegFile(const CString& szPath, TIFF* output, uint32& directoryNumber, uint32 directoryCount);

// Merge a gray jpeg picture using libjpeg at www.ijg.org
void mergeGrayJpegFile(struct jpeg_decompress_struct &cinfo, ExifData *ed, TIFF *output, uint32& directoryNumber, uint32 directoryCount);

// Merge a color jpeg picture using libjpeg at www.ijg.org
void mergeColorJpegFile(struct jpeg_decompress_struct &cinfo, ExifData *ed, TIFF *output, uint32& directoryNumber, uint32 directoryCount);

// Get the exif information
ExifData* getExifInfo(const char* filePath);
ExifData* getExifInfo(const CString& szPath);

// Get the maker of jpeg files
bool getMakerInfo(char* maker, ExifData* ed);

// Show the tag name and contents if the tag exists
// For debug
void showTag(ExifData *ed, ExifIfd ifd, ExifTag tag);

// Get the value of the tag
string getTagValue(ExifData *ed, ExifIfd ifd, ExifTag tag);

// Get the X/Y Resolution
float getResolution(ExifData *ed, ExifIfd ifd, ExifTag tag);

// Get the resolution unit
uint16_t getResolutionUnit(ExifData *ed, ExifIfd ifd, ExifTag tag);

// Set the resolution information
void setResolutionInfo(const struct jpeg_decompress_struct &cinfo, ExifData *ed, TIFF *output);

#endif